﻿using System;
using System.IO;

namespace MDD.PML.Utility
{
    internal class ArgsValidator
    {
        internal bool ValidatePIM2PSM(ArgsParser argsParser)
        {
            if (argsParser.IsNullOrEmpty(Arguments.filename.ToString()))
            {
                Logger.Log("filename argument is missing or empty");
                return false;
            }
            else
            {
                string fileName = argsParser[Arguments.filename.ToString()];
                if (!this.FileExists(fileName))
                {
                    Logger.Log("File does not exist");
                    return false;
                }

                if (!this.ValidateFileExtension(fileName, "cpml"))
                {
                    Logger.Log("Extension must be .cpml");
                    return false;
                }

                if (!this.ValidateModelType(argsParser[Arguments.modeltype.ToString()]))
                {
                    Logger.Log("ModelType must be upml, spml or all");
                    return false;
                }
            }

            return true;
        }

        internal bool ValidatePSM2Umbraco(ArgsParser argsParser)
        {
            if (argsParser.IsNullOrEmpty(Arguments.filename.ToString()))
            {
                Logger.Log("filename argument is missing or empty");
                return false;
            }
            else
            {
                string fileName = argsParser[Arguments.filename.ToString()];
                if (!this.FileExists(fileName))
                {
                    Logger.Log("File does not exist");
                    return false;
                }

                if (!this.ValidateFileExtension(fileName, "upml"))
                {
                    Logger.Log("Extension must be .upml");
                    return false;
                }
            }

            return true;
        }

        internal bool ValidateUmbraco2PSM(ArgsParser argsParser)
        {
            if (argsParser.IsNullOrEmpty(Arguments.filename.ToString()))
            {
                Logger.Log("filename argument is missing or empty");
                return false;
            }
            else
            {
                string fileName = argsParser[Arguments.filename.ToString()];
                if (!this.FileExists(fileName))
                {
                    Logger.Log("File does not exist");
                    return false;
                }

                if (!this.ValidateFileExtension(fileName, "xml"))
                {
                    Logger.Log("Extension must be .xml");
                    return false;
                }
            }

            return true;
        }

        internal bool ValidatePSM2PIM(ArgsParser argsParser)
        {
            if (argsParser.IsNullOrEmpty(Arguments.filename.ToString()))
            {
                Logger.Log("filename argument is missing or empty");
                return false;
            }
            else
            {
                string fileName = argsParser[Arguments.filename.ToString()];
                if (!this.FileExists(fileName))
                {
                    Logger.Log("File does not exist");
                    return false;
                }

                if (!this.ValidateFileExtension(fileName, "upml"))
                {
                    Logger.Log("Extension must be .upml");
                    return false;
                }

            }

            return true;
        }


        private bool ValidateModelType(string modelType)
        {
            if (string.IsNullOrEmpty(modelType))
                return true;

            foreach (ModelType enumModelType in Enum.GetValues(typeof(ModelType)))
            {
                if (modelType.ToLower() == enumModelType.ToString().ToLower())
                    return true;
            }

            return false;
        }

        private bool FileExists(string fileName)
        {
            if (!Path.IsPathRooted(fileName))
                fileName = Path.Combine(Directory.GetCurrentDirectory(), fileName);

            return File.Exists(fileName);
        }

        private bool DirExists(string fileLocation)
        {
            if (Path.IsPathRooted(fileLocation))
            {
                return Directory.Exists(Path.GetPathRoot(fileLocation));
            }

            // Only file name specified - use current directory
            return true;
        }

        private bool ValidateFileExtension(string fileName, string extension)
        {
            if (!extension.StartsWith("."))
                extension = "." + extension;

            return (Path.GetExtension(fileName) == extension);
        }
    }
}
